package com.zhou.common.base.mq.mq.local;


import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@ConditionalOnProperty(prefix = "rocketmq", name = "enable", havingValue = "true")
@EnableConfigurationProperties(RocketMqProperties.class)
public class LocalRocketMqConfig {

    @Autowired
    private RocketMqProperties rocketMqProperties;

    @Bean(initMethod = "start", destroyMethod = "shutdown")
    @ConditionalOnProperty(prefix = "rocketmq.producer", name = "enable", havingValue = "true")
    public DefaultMQProducer defaultProducer() throws MQClientException {
        DefaultMQProducer producer = new DefaultMQProducer(rocketMqProperties.getProducer().getGroup());
        producer.setNamesrvAddr(rocketMqProperties.getNameServer());
        producer.setVipChannelEnabled(false);
        producer.setMaxMessageSize(rocketMqProperties.getProducer().getMaxMessageSize());
        producer.setSendMsgTimeout(rocketMqProperties.getProducer().getSendMessagetimeout());
        producer.setRetryTimesWhenSendAsyncFailed(rocketMqProperties.getProducer().getRetryTimesWhenSendAsyncFailed());
        producer.setCompressMsgBodyOverHowmuch(rocketMqProperties.getProducer().getCompressMessageBodyThreshold());
        producer.setRetryTimesWhenSendFailed(rocketMqProperties.getProducer().getRetryTimesWhenSendFailed());
        return producer;
    }


}
